package com.object.HttpClient;

import com.Inter.HttpClient.POST;
import com.function.UnicodeToCN;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpEntityContainer;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.concurrent.Future;

public class HttpClient implements POST {
    @Override
    public void POST(String body,String Cookie) {
        // 1.生成客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2.请求
        HttpPost request1 = new HttpPost("https://mps.ksyun.com/data/loop");
        // 2-1 设置Header
        request1.addHeader("Accept","application/json, text/plain, */*");
        request1.addHeader("Accept-Encoding","gzip, deflate, br");
        request1.addHeader("Accept-Language","zh-CN,zh;q=0.9");
        request1.addHeader("Content-Type","application/json");
        request1.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36");
        request1.addHeader("Cookie",Cookie);
    // 2-2 设置Body
        request1.setEntity(new StringEntity(body, Charset.forName("UTF-8")));
        try{
            // 接受响应
            CloseableHttpResponse response1 = httpClient.execute(request1);
            int Code=response1.getCode();
            //System.out.println(response1.getCode()+": "+response1.getEntity());
            if (Code==200){
                System.out.println(response1.toString());
                // 获取响应Body
                HttpEntity entity = response1.getEntity();
                // 打印响应Body
                String response_body = EntityUtils.toString(entity,"utf-8");
                // unicode 转中文
                String s=new UnicodeToCN().unicodeDecode(response_body);
                System.out.println(s);
            }else{
                System.out.println("发送失败，请检查网络");
            }
            httpClient.execute(request1, HttpEntityContainer::getEntity);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("请求错误");
        }


    }
}
